package action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import common.BaseAction;
import interfaces.IFunctionService;
import interfaces.IGlobalService;
import interfaces.IOperatorService;
import util.LogUtil;

public class CoreAction extends BaseAction {

	private static final long serialVersionUID = 7507534255609348887L;

	private IGlobalService globalService;
	private IOperatorService operatorService;
	private IFunctionService functionService;
	private String sqlText;
	private List<LinkedHashMap<String, Object>> sqlResult;

	private Integer operatorCount;
	private Integer functionCount;
	private String logType;
	private String logName;
	private List<File> operateList;
	private List<File> loginList;

	public String index() throws Exception {
		return SUCCESS;
	}

	public String about() throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("optState", "1");
		paramMap.put("funState", "1");
		operatorCount=operatorService.record(paramMap);
		functionCount=functionService.record(paramMap);
		return SUCCESS;
	}

	public String log() throws Exception {
		operateList = LogUtil.list("operate");
		loginList = LogUtil.list("login");
		return SUCCESS;
	}

	public String sql() throws Exception {
		try {
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sqlText);
			querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			sqlResult = resultSet.getResultList();
			pageInfo = resultSet.getPageInfo();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.toString());
			return ERROR;
		}
	}

	public String download() throws Exception {
		String result = LogUtil.get(logType, logName);
		inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"), 0, result.getBytes("UTF-8").length);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IGlobalService getGlobalService() {
		return globalService;
	}

	public void setGlobalService(IGlobalService globalService) {
		this.globalService = globalService;
	}

	public String getSqlText() {
		return sqlText;
	}

	public void setSqlText(String sqlText) {
		this.sqlText = sqlText;
	}

	public List<LinkedHashMap<String, Object>> getSqlResult() {
		return sqlResult;
	}

	public void setSqlResult(List<LinkedHashMap<String, Object>> sqlResult) {
		this.sqlResult = sqlResult;
	}

	public List<File> getLoginList() {
		return loginList;
	}

	public void setLoginList(List<File> loginList) {
		this.loginList = loginList;
	}

	public String getLogType() {
		return logType;
	}

	public void setLogType(String logType) {
		this.logType = logType;
	}

	public String getLogName() {
		return logName;
	}

	public void setLogName(String logName) {
		this.logName = logName;
	}

	public List<File> getOperateList() {
		return operateList;
	}

	public void setOperateList(List<File> operateList) {
		this.operateList = operateList;
	}

	public IOperatorService getOperatorService() {
		return operatorService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

	public IFunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}

	public Integer getOperatorCount() {
		return operatorCount;
	}

	public void setOperatorCount(Integer operatorCount) {
		this.operatorCount = operatorCount;
	}

	public Integer getFunctionCount() {
		return functionCount;
	}

	public void setFunctionCount(Integer functionCount) {
		this.functionCount = functionCount;
	}

	

	
	
}
